package com.android.scanner.fragment;

import java.util.ArrayList;
import java.util.List;

import com.android.scanner.R;
import com.android.scanner.adapter.CommonAdapter;
import com.android.scanner.utils.Constant;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;

public class LinkFragment extends BaseFragment implements OnClickListener,
		OnItemSelectedListener, OnItemClickListener {

	private Handler handler;
	private Context context;
	private int[] selects = { R.string.bluetooth, R.string.wifi,
			R.string.usb, R.string.udisk };
	private List<String> list;
	private CommonAdapter adapter = null;
	private ListView lv;
	private static int curPosition = 0;
	private Button btnLeft, btnRight;
	private static LinkFragment mSelft = null;

	public static LinkFragment getInstance(Handler handler) {
		if (mSelft != null)
			return mSelft;
		return new LinkFragment(handler);
	}

	public LinkFragment(Handler handler) {
		this.handler = handler;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context = getActivity();
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_link, null);
		initViews(view);
		return view;
	}

	private void initViews(View view) {
		// TODO Auto-generated method stub
		btnLeft = (Button) view.findViewById(R.id.btn_left);
		btnRight = (Button) view.findViewById(R.id.btn_right);
		btnLeft.setOnClickListener(this);
		btnRight.setOnClickListener(this);

		list = new ArrayList<String>();
		for (int i = 0; i < selects.length; i++) {
			list.add(context.getResources().getString(selects[i]));
		}

		lv = (ListView) view.findViewById(R.id.lv);

		adapter = new CommonAdapter(context, list, lv, curPosition);
		lv.setAdapter(adapter);

		lv.requestFocus();
		lv.setSelection(curPosition);

		lv.setOnItemSelectedListener(this);
		lv.setOnItemClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0.getId() == R.id.btn_left) {

		} else if (arg0.getId() == R.id.btn_right) {
			handler.sendEmptyMessage(Constant.MSG_BACK_SETTINGS);
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		curPosition = arg2;
		adapter.updateHLPosition(curPosition);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean setKeyEvent(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			btnLeft.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			btnRight.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_F5) {
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			doSomething(curPosition);
		}
		return super.setKeyEvent(keyCode, event);
	}

	private void doSomething(int position) {
		// TODO Auto-generated method stub
		switch (position) {


		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		doSomething(curPosition);
	}

}
